﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SharedLib.BasicServices.Plugin.Configuration;
using Tracy.DataModel;
using TracyGUI.UIComponent;
using Tracy;
using Tracy.Messages;
using Tracy.Handlers;
using SharedLib.BasicServices.MessageFramework;
using Tracy.DataModel.UltraExplorer;
using SharedLib.XmlDataModel;
using SharedLib.DataServices;

namespace TracyGUI
{
    public partial class FormMain
    {
        //TreeNode _rootNode = null;
        UltraExplorer<MediaFile> _ueMediaFile;
        List<MediaFile> _ultraExplorerSelectedFiles = null;
        TabPageEntry _ultraExplorerEntryPage = null;
        
        void LoadFileUltraExplorer()
        {
            //Init UE
            _ueMediaFile = new UltraExplorer<MediaFile>(treeViewFileUE, "All Files");
            _ueMediaFile.DisplayCondition = file => "true".Equals(file.Visible, StringComparison.OrdinalIgnoreCase);
            _ueMediaFile.GetAllItemScope = () =>
            {
                List<MediaFile> rtn = new List<MediaFile>();
                foreach (ListBoxItemEntry item in listBoxEntries.Items)
                {
                    Entry entry = item.Data as Entry;
                    if (entry != null)
                    {
                        rtn.AddRange(TracyBus.Instance.MediaFileProvider.GetItemByEntry(entry));
                    }
                }
                return rtn;
            };

            //Load classifier sets to dropdown list
            foreach (string classifierSet in TracyBus.Instance.FileClassifierProvider.ClassifierSets.Keys)
            {
                comboBoxFileUE.Items.Add(classifierSet);
            }
            if (comboBoxFileUE.Items.Count > 0) comboBoxFileUE.SelectedIndex = 0;
        }

        private void comboBoxFileUE_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxFileUE.SelectedIndex >= 0)
            {
                if (_ueMediaFile.IsLastGeneratedNode(_ueMediaFile.RootNode))
                {
                    return;
                }
                if (comboBoxFileUE.SelectedIndex >= 0)
                {
                    _ueMediaFile.CurrentClassifierSet = TracyBus.Instance.FileClassifierProvider.ClassifierSets[comboBoxFileUE.SelectedItem.ToString()];
                }
                else
                {
                    _ueMediaFile.CurrentClassifierSet = new List<string>();
                }

                _ueMediaFile.ReloadTree();
            }
        }

        private void treeViewFileUE_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Action != TreeViewAction.Unknown)
            {
                _ultraExplorerSelectedFiles = _ueMediaFile.GetMatchedItems(e.Node);
                if (_ultraExplorerEntryPage == null)
                {
                    _ultraExplorerEntryPage = CreateEntryTab(TabPageEntry.TABPAGE_NAME_UE);
                    _tabControlContent.TabPages.Add(_ultraExplorerEntryPage);
                }
                else LoadDataToEntryTab(_ultraExplorerEntryPage, TabPageEntry.TABPAGE_NAME_UE);
                _tabControlContent.SelectedTab = _ultraExplorerEntryPage;
            }
        }
        private void treeViewFileUE_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            if (_ueMediaFile.IsLastGeneratedNode(e.Node))
            {
                e.Node.Nodes.Clear();
                _ueMediaFile.GenerateFilterChildNodes(e.Node);
            }
        }

        
    }
}